// index.js
// 获取应用实例
const app = getApp()
import http from "../../utils/http"
import util from "../../utils/util"
Page({
  data: {
    checked: false,
    show:false,
    menuH: '32px',
    safeTop: `40px`,
    lastAddress: '',
    list:[],
    finish: false,
    loading: false,
    page: 1,
    orderCount: [],
    token: wx.getStorageSync('token'),
    code: '',
    config: ''
  },
  // 服务时间
  toServerTime(){
    util.authSkip('/service/serverTime/serverTime')
  },
  detail(e){
    util.skip('/pages/orderDetail/orderDetail?id='+e.currentTarget.dataset.id)
  },
  // 事件处理函数
  onChange({ detail }) {
    if(!wx.getStorageSync('token')) {
      util.skip('/service/login/login')
      return
    }
    http.post('skill/updateskillinfo',{type: 'update', is_rest: Number(!detail)}).then(res => {
      this.setData({
        checked: detail
      })
    })
  },
  onClose(){
    this.setData({
      show: false
    })
  },
  notice(){
    wx.requestSubscribeMessage({
      tmplIds: [app.globalData.templateconfig.skill_order_template,app.globalData.templateconfig.skill_sales_template],
      success :(res) =>{ 
        console.log(res)
      },
      fail :(e) =>{ 
        console.log(e)
      },
      complete :(res) =>{ 
        this.changeAddress()
      }
    })
  },
  changeAddress(){
    if(!wx.getStorageSync('token')) {
      util.skip('/service/login/login')
      return
    }
    http.post('skill/updateskillinfo',{
      type: 'update',
       province: this.data.address.province,
       city: this.data.address.city,
       district: this.data.address.district,
       address: this.data.address.name,
       lng: this.data.address.lng,
       lat: this.data.address.lat,
    }).then(res => {
      this.setData({
        show: true,
        lastAddress: this.data.address.city + ' ' +this.data.address.name
      })
  })
  },
  getInfo(){
    if(!wx.getStorageSync('token')) return
    http.post('skill/updateskillinfo','',!this.data.code).then(res => {
      if(res.data){
        this.setData({
          checked: res.data.is_rest === 0,
          lastAddress: res.data.city+ ' ' + res.data.address,
          code: res.data.code
        })
      }
    })
    http.post('skill/skillhome','',!this.data.orderCount).then(res => {
      this.setData({
        orderCount: res.data.orderCount
      })
  })
  },
  order(){
    wx.switchTab({
      url: '/pages/order/order'
    })
  },
  reload(){
    this.setData({
      list: [],
      page: 1,
      finish: false
    })
    this.getList()
  },
  more(){
    this.setData({
      page: ++this.data.page
    })
    this.getList()
  },
  getList(){
    if(!wx.getStorageSync('token')) return
    if(this.data.finish){
      return 
    }
    if(this.data.loading){
      return 
    }
    this.setData({
      loading: true
    })
    let data = {
      page: this.data.page,
      not_finish: 1
    }
    http.get('order/skillorderlist', data).then(res => {
      if(res.data.length === 0){
        this.setData({
          finish: true
        })
      }
      let arr = this.data.list.concat(res.data)
      this.setData({
        list: arr
      })
      
    }).finally(res => {
      this.setData({
        loading: false
      })
    })
  },
  knowLocation(){
    wx.navigateTo({
      url: '/service/login/login'
    })
  },
  toTraining() {
    wx.navigateTo({
      url: '/pages/training/training'
    })
  },
  testbtn(){
    this.setData({
      loading:true
    })
    setTimeout(()=>
    {
     this.setData({
      loading:false
     })
    }, 2000)
    console.log(this.data.loading)
  },
  onLoadAddress(option){
    this.setData({
      address: app.globalData.address
    })
  },
  onLoadConfig(option){
    this.setData({
      config: app.globalData.config
    })
  },
  onLoad() {
    this.setData({
      safeTop: `${app.globalData.safeTop}px`,
      menuH: `${app.globalData.menuH}px`,
    })
    
    
  },
  onShow(){
    this.setData({
      token: wx.getStorageSync('token')
    })
    this.getInfo()
    this.reload()
  },
  
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
    return {}
  }
})
